<?php
/**
 * quick_curd
 * ============================================================================
 * 版权所有 2024-2099 wltx666.com，并保留所有权利。
 * 网站: https://wltx666.com
 * ============================================================================
 * Author: 草上飞
 * Email: wltx@alipay168.cn
 * QQ: 2719875001
 * Date: 2025/08/10
 * 描述: api_xxx 代表前端接口,其它代表后端接口
 */

namespace app\result\auto;

use app\model\auto\AdminLoginLog;
use app\result\BaseResult;


class AdminLoginLogResult extends BaseResult
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new AdminLoginLog();
    }

    // 表单字段
    public function form_fields(): array
    {
        return [
            ];
    }
    //列表页面配置
    public function list_config(): array
    {
         return $this->success_return('success', [
            'form_fields' => method_exists($this, 'form_fields') ? $this->form_fields() : [],
            'el_table_column' => method_exists($this, 'el_table_column') ? $this->el_table_column() : [],
            'search_fields' => method_exists($this, '_searchField') ? $this->_searchField() : []
        ]);
    }

    // 表格列
    public function el_table_column(): array
    {
        return [
            ['prop' => 'id','type' => 'text','label' => 'id','minWidth' => '120','align' => 'center',],
            ['prop' => 'admin_id','type' => 'text','label' => '管理员id','minWidth' => '120','align' => 'center',],
            ['prop' => 'expire','type' => 'text','label' => 'token有效期','minWidth' => '120','align' => 'center',],
            ['prop' => 'ip','type' => 'text','label' => '登录ip','minWidth' => '120','align' => 'center',],
            ['prop' => 'created_at','type' => 'text','label' => '登录时间','minWidth' => '120','align' => 'center',],
            ['prop' => '','type' => 'buttons','label' => '编辑','minWidth' => '130','align' => 'center','fixed' => 'right',
            'buttons' => [ [
            'label' => '删除','action' => 'del','icon' => 'el-icon-delete','type' => 'danger','size' => 'mini',],]],
            ];
    }

    //列表搜索字段
    private function _searchField(): array
    {
        return [
            ['name' => 'ip','label' => '登录ip','type' => 'input','placeholder' => '登录ip',],
            ['name' => 'created_at','label' => '登录时间','type' => 'daterange','placeholder' => '登录时间',],
            ];
    }

    // 获取公共条件限制
    private function _where($args = []): array
    {
        $where = [];
        
                if (!empty($args['ip'])) {
                    $where[] = ['ip', '=', $args['ip']];
                }
                    if (!empty($args['created_at'])) {
                    $where[] = ['created_at', 'between', [$args['created_at'][0].' 00:00:00', $args['created_at'][1].' 23:59:59']];
                    }
        return $where;
    }

    public function list($args = []): array
    {
        $page = intval($args['page'] ?? 1);
        $pageSize = intval($args['limit'] ?? 10);
        $sort_by = trim($args['sort_by'] ?? 'id desc');
        $where = $this->_where($args);
        $total = $this->model->data_count($where);
        $data = $this->model->data_list($where, '*', $sort_by, $page, $pageSize);
        return $this->success_return('success', [
            'total' =>$total,
            'list' => $data,
        ]);
    }


    // 删除数据
    //todo 自己去写软删除还是硬删除
    public function del_data($args): array
    {
        $ids = $args['ids'] ?? [];
        if (!$ids) {
            $ids = !empty($args['id']) ? [$args['id']] : [];
        }
        if (!$ids) {
            return $this->error_return('请选择数据');
        }
         $data = $this->model->data_list_nopage([["id", 'in', $ids]], '*');
        if (!$data) {
            return $this->error_return('数据不存在');
        }
        //判断是否存在del_time
        if (isset($data[0]['del_time'])) {
            $res = $this->model->data_update([["id", 'in', $ids]], ['del_time' => time()]);
        } else {
            $res = $this->model->data_del([["id", 'in', $ids]]);
        }
        if (!$res) {
            return $this->error_return('删除失败');
        }
        admin_log('删除客服数据', $data);
        return $this->success_return('删除成功');
    }

}